package com.zhixiang.xaec.service.system.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.xaec.dao.system.ForderDao;
import com.zhixiang.xaec.entity.system.Forder;
import com.zhixiang.xaec.service.system.ForderService;


@Service
public class ForderServiceImpl extends BaseServiceImpl<Forder, Long> implements ForderService
{
    @Autowired
    private ForderDao dao;
    
    public String saveForder(Forder forder)
    {
        Long localLong = forder.getParentId();
        if (localLong.longValue() < 1L)
        {
          localLong = new Long(0L);
        }
        save(forder);
        return null;
    }

    public List<Forder> findByParentId(Long id){
       return dao.findByParentId(id);
    }
    
    /**
     * 前台得到tree节点集合
     */
    public List<TreeObj> getTreeObj(Long id){
        List<TreeObj> treelist = new ArrayList<TreeObj>();
        
        Forder dept = get(id);
        TreeObj tobj= new TreeObj();
        tobj.setId(dept.getForId().toString());
        tobj.setText(dept.getForName());
        tobj.setExpanded(true);
        treelist.add(tobj);
        findchildren(tobj);
        return treelist;
    }
    
    private void findchildren(TreeObj treeObj){
        List<Forder> dList = findByParentId(Long.parseLong(treeObj.getId()));
        if(dList != null) {
            List<TreeObj> treelist = new ArrayList<TreeObj>();
            for (int i = 0; i < dList.size(); i++ )
            {
                Forder forder =  dList.get(i);
                TreeObj tobj= new TreeObj();
                tobj.setId(forder.getForId().toString());
                tobj.setText(forder.getForName());
                List<Forder> dd = findByParentId(forder.getForId());
                if(dd.size()==0){
                    tobj.setLeaf(true);
                }
                treelist.add(tobj);
            }
            treeObj.setChildren(treelist);
            
            for (int i = 0; i < treelist.size(); i++ )
            {
                TreeObj tbj = treelist.get(i);
                findchildren(tbj);
            }
        }
    }
    
    /**
     * 
     * Description: 根据当前文件夹对象 得到他的相对路径
     * @param forder 当前文件夹对象
     * @return 
     * @see
     */
    public String getPath(Forder forder){
        String path="";
        
        List<String> list = new ArrayList<String>();
        forder = get(forder.getForId());
        while(forder.getParentId()>=-1){
            list.add("/"+forder.getRealName().trim() );
            if(forder.getParentId()!=-1){
                forder = get(forder.getParentId());
            }else{
                break;
            }
        }
        for(int i =list.size()-1;i>=0;i--){
            path += list.get(i);
        }
        return path;
    }

    @Resource(type=ForderDao.class)
    public void setBaseDao(BaseDao<Forder, Long> dao)
    {
        setDao(dao);
    }
    
}
